package simulation;

import java.security.AllPermission;
import java.util.*;

import javax.imageio.spi.RegisterableService;

public class Tomasulo {

	static int cycles = 0;
	static int PC = 0;

	static int id = 0;
	
	public static int addCycles ;
	public static int multCycles;
	public static int divCycles;

	static int addLatency = 1; // dummy variable
	static int multLatency = 4; // dummy variable
	static int divLatency = 10; // dummy variable
	static int defaultLatency = 3; // dummy variable for BEQ,JMP,NAND
	public static int finalCycle = 0;

	static boolean branchFlag = false;

	public static Simulator simulator;
	// int[] renamedMap = new int[10];
	
	static int superScalar = 4;
	static ArrayList<String> decodeArray = new ArrayList<String>(5);// Simulator.hardwarOrg.waySuperscalar;
	static ArrayList<String> dispatchArray = new ArrayList<String>(5);
	static ArrayList<String> executeArray = new ArrayList<String>(5);
	static ArrayList<Integer> branchArray = new ArrayList<Integer>(5);
	static ArrayList<String> fetchArray = new ArrayList<String>(5);
	static ArrayList<String> branchString= new ArrayList<String>(5);
	
	public Tomasulo(Simulator simulator){
		this.simulator=simulator;
		this.PC=simulator.programStartingAddress;
		superScalar = simulator.ways;
		for (int i = 16; i < 26; i++) {
			simulator.emptyReigsters.add("R" + i);
		}
		simulator.map.put("R0", 0);
		simulator.map.put("R1", 1);
		simulator.map.put("R2", 2);
		simulator.map.put("R3", 3);
		simulator.map.put("R4", 4);
		simulator.map.put("R5", 5);
		simulator.map.put("R6", 6);
		simulator.map.put("R7", 7);

		fetch();
		
	}
	
	static void fetch() {

		int i = 0;
		int time = 0;
		if (fetchArray.isEmpty()) {
			while (i < superScalar) {

				if (PC < simulator.program.length
						+ simulator.programStartingAddress) {
					Object[] timeAndValue = simulator.searchCache(PC);

					fetchArray.add((String) timeAndValue[1] + "," + PC);
					time += (Integer) timeAndValue[0];

					PC++;
				} else {
					break;
				}
				i++;
			}

		}

		for (int j = 0; j < fetchArray.size(); j++) {

			decodeArray.add(fetchArray.get(j));
		}
		fetchArray.clear();
		// cycles+=time;

		cycles += time;

		// System.out.println(decodeArray.toString());
		decode();

		if (PC < simulator.program.length + simulator.programStartingAddress)
			fetch();

	}

	static void decode() {

		if (dispatchArray.isEmpty()) {		
			while (!decodeArray.isEmpty()) {
				int i = 0;
				while (i < decodeArray.size()) {
					System.out.println(decodeArray.get(i));
					String[] inst = decodeArray.get(i).split(" ");
					String opcode = inst[0];
					String[] operands = inst[1].split(",");

					if (simulator.reservationStations.hasPlace(decodeArray
							.get(i))) {
						if (!simulator.rob.isFull()) {

							if (!opcode.equals("RET")&&!opcode.equals("JMP")&&checkOP(operands[1])
									&& containsValue(operands[1])) {
								operands[1] = getKey(operands[1]);

							}

							if (!opcode.equals("RET")&&!opcode.equals("JMP")&&checkOP(operands[2])
									&& containsValue(operands[2])) {
								operands[2] = getKey(operands[2]);

							}
							if (!(opcode.equals("BEQ") || opcode.equals("JMP")
									|| opcode.equals("JALR") || opcode
									.equals("RET") || opcode.equals("SW"))) {

								ArrayList<String> array = new ArrayList<String>();

								array.add(simulator.emptyReigsters.peek());
								array.add(operands[0]);

								simulator.registerMap.add(array);
							
								simulator.map.put(
										simulator.emptyReigsters.peek(),
										simulator.map.get(operands[0]));
								operands[0] = simulator.emptyReigsters
										.removeFirst();
							} else {
								if (checkOP(operands[0])
										&& containsValue(operands[0])) {
									operands[0] = getKey(operands[0]);

								}

							}
							// renaming done and inserted in register map as
							// well as map
							if(opcode.equals("RET")){
								decodeArray.set(i, opcode + " " + operands[0] + ","
										+ operands[1]);
	
							}
							else
								if(opcode.equals("JALR")|| opcode.equals("JMP")){
									decodeArray.set(i, opcode + " " + operands[0] + ","
											+ operands[1] + "," + operands[2] 
											);
											
								}else{
							decodeArray.set(i, opcode + " " + operands[0] + ","
									+ operands[1] + "," + operands[2] + ","
									+ operands[3]);
								}
							
							dispatchArray.add(decodeArray.get(i));// +","+cycles);
							decodeArray.remove(i);
							i = i - 1;

						} else {
							System.out.println("stall");
							// STALL
						}

					}
					i++;

				}
				cycles++;
				dispatch();
			}
		}
	}

	static void dispatch() {

		// dispatchArray = instructions;
		if (executeArray.isEmpty()) {

			while (!dispatchArray.isEmpty()) {
				int i = 0;
				while (i < dispatchArray.size()) {
					
					String[] inst = dispatchArray.get(i).split(" ");
					String opcode = inst[0];
					String[] operands = inst[1].split(",");

					int R1;
					int R2;
					boolean R1bit = false;
					boolean R2bit = false;

					System.out.println(dispatchArray.get(i)+"INSTRUCTION");
					
					if (!opcode.equals("RET")&&checkOP(operands[1])) {
						if (!containsKey1(operands[1])) {
							R1 = simulator.registerFile[simulator.map
									.get(operands[1])];
							R1bit = true;
						} else {

							ROBentry re = null;
							for (int j = 0; j < simulator.rob.ROB.size(); j++) {
								if (simulator.rob.ROB.get(j).register
										.equalsIgnoreCase(operands[1])) {
									re = simulator.rob.ROB.get(j);
									break;
								}
							}
							if (re!=null &&re.flag) {
								R1 = re.value;
								R1bit = true;
							}else{
								if(re==null)
									R1bit=true;
							}

						}
					} else {
						R1bit = true;
					}

					if (opcode.equals("BEQ") || opcode.equals("JMP")

							|| opcode.equals("JALR") || opcode.equals("RET")||opcode.equals("SW") ) {

					

						if (checkOP(operands[0])) {

							if (!containsKey1(operands[0])) {
								R2 = simulator.registerFile[simulator.map
										.get(operands[0])];
								R2bit = true;
							} else {

								ROBentry re = null;

								for (int j = 0; j < simulator.rob.ROB.size(); j++) {
									if (simulator.rob.ROB.get(j).register
											.equalsIgnoreCase(operands[0])) {
										re = simulator.rob.ROB.get(j);
										break;
									}
								}

								if (re.flag) {
									R2 = re.value;
									R2bit = true;

								}

							}
						} else {
							R2bit = true;
						}

					} else {

						if (!opcode.equals("RET")&&checkOP(operands[2])) {
							if (!containsKey1(operands[2])) {
								R2 = simulator.registerFile[simulator.map
										.get(operands[2])];
								R2bit = true;
							} else {
								ROBentry re = null;
								for (int j = 0; j < simulator.rob.ROB.size(); j++) {
									if (simulator.rob.ROB.get(j).register
											.equalsIgnoreCase(operands[2])) {
										re = simulator.rob.ROB.get(j);
										break;
									}
								}
							
								if (re!=null && re.flag) {
									R2 = re.value;
									R2bit = true;

								}else if(re==null)
									R2bit=true;

							}
						} else {
							R2bit = true;
						}
					}
					boolean flag = true;
					for (int j = 0; j < simulator.rob.ROB.size(); j++) {

						if (simulator.rob.ROB.get(j).register
								.equalsIgnoreCase(operands[0])) {

							flag = false;
							break;
						}
					}

					if (flag && !(opcode.equals("BEQ") || opcode.equals("JMP")
							|| opcode.equals("JALR") || opcode.equals("RET") || opcode.equals("SW"))) {
						ROBentry robe = new ROBentry(id, false, -1,
								operands[0], opcode);
						simulator.rob.ROB.add(robe);

						ReservationStationEntry rse = new ReservationStationEntry(
								id, opcode, "issue", operands[0], operands[1],
								operands[2], R1bit, R2bit, "", "");
					//	System.out.println(rse.toString());
						simulator.reservationStations.RS.add(rse);
						
						if (branchFlag){
							branchArray.add(new Integer(id));
							
							}
							id++;

					}

					// System.out.println(simulator.rob.ROB.toString());
					if (R1bit && R2bit && i == 0) {
						branchString.add(dispatchArray.get(i));
					
						executeArray.add(dispatchArray.get(i));
						dispatchArray.remove(i);

						i = i - 1;
					}

					i++;

					if (opcode.equals("BEQ") || opcode.equals("JMP")
							|| opcode.equals("JALR") || opcode.equals("RET")) {
						branchFlag = true;
					}
				}
				cycles++;
				execute();
			}
		}
	}

	// both sources must be ready
	static void updateStation(String operands, int result, int i) {

		for (int j = 0; j < simulator.rob.ROB.size(); j++) {

			if (operands.equalsIgnoreCase((simulator.rob.ROB.get(j).register))) {

				simulator.rob.ROB.get(j).flag = true;
				simulator.rob.ROB.get(j).value = result;

			}
		}

		for (int j = 0; j < simulator.reservationStations.RS.size(); j++) {
			if (operands.equalsIgnoreCase((simulator.reservationStations.RS
					.get(j).resultOperand))) {
				simulator.reservationStations.RS.get(j).done = true;
				simulator.reservationStations.RS.get(j).status = "Done";

			}
		}

		for (int j = 0; j < simulator.reservationStations.RS.size(); j++) {
			if (operands.equalsIgnoreCase(simulator.reservationStations.RS
					.get(j).sourceOperand1)) {
				simulator.reservationStations.RS.get(j).sourceReady1 = true;
			}
			if (operands.equalsIgnoreCase(simulator.reservationStations.RS
					.get(j).sourceOperand2)) {
				simulator.reservationStations.RS.get(j).sourceReady2 = true;
			}
		}
	}

	static void execute() {

		ArrayList<Integer> allCycles = new ArrayList<Integer>(); 
		
		ArrayList<Integer> smallest = new ArrayList<Integer>();
		while (!executeArray.isEmpty()) {
			
			for (int i = 0; i < executeArray.size(); i++) {
				

				
				String[] inst = executeArray.get(i).split(" ");
				String opcode = inst[0];
				String[] operands = inst[1].split(",");
				int currentCycle = 0;

				// simulator.execute(executeArray.get(i));
				int result;
				ROBentry robe;
				ReservationStationEntry rse;

				if (opcode.equals("ADD")) {

					result = simulator.registerFile[simulator.map
							.get(operands[1])]
							+ simulator.registerFile[simulator.map
									.get(operands[2])];

					updateStation(operands[0], result, i);

					currentCycle = cycles + simulator.reservationStations.addsubCycles;
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add(simulator.reservationStations.addsubCycles);
					}
					executeArray.remove(i);
					removeKey(operands[0]);
				}

				if (opcode.equals("SUB")) {

					result = simulator.registerFile[simulator.map
							.get(operands[1])]
							- simulator.registerFile[simulator.map
									.get(operands[2])];

					updateStation(operands[0], result, i);
					currentCycle = cycles + simulator.reservationStations.addsubCycles;
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add(simulator.reservationStations.addsubCycles);
					}
					executeArray.remove(i);
					removeKey(operands[0]);
				}

				if (opcode.equals("ADDI")) {
					result = simulator.registerFile[simulator.map
							.get(operands[1])] + Integer.parseInt(operands[2]);
					updateStation(operands[0], result, i);
					currentCycle = cycles + simulator.reservationStations.addsubCycles;
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add(simulator.reservationStations.addsubCycles);
					}
					executeArray.remove(i);
					removeKey(operands[0]);
				}
				if (opcode.equals("NAND")) {
					result = ~(simulator.registerFile[simulator.map
							.get(operands[1])] & simulator.registerFile[simulator.map
							.get(operands[2])]);
					updateStation(operands[0], result, i);
					currentCycle = cycles + defaultLatency;
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add(defaultLatency);
					}
					executeArray.remove(i);
					removeKey(operands[0]);
				}
				if (opcode.equals("MUL")) {
					
					result = simulator.registerFile[simulator.map
							.get(operands[1])]
							* simulator.registerFile[simulator.map
									.get(operands[2])];

					updateStation(operands[0], result, i);
					currentCycle = cycles + simulator.reservationStations.mulCycles;
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add(simulator.reservationStations.mulCycles);
					}
					executeArray.remove(i);
					removeKey(operands[0]);
				}
				if (opcode.equals("DIV")) {
					result = simulator.registerFile[simulator.map
							.get(operands[1])]
							/ simulator.registerFile[simulator.map
									.get(operands[2])];
					updateStation(operands[0], result, i);
					currentCycle = cycles + simulator.reservationStations.divCycles;
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add(simulator.reservationStations.divCycles);
					}
					executeArray.remove(i);
					removeKey(operands[0]);
				}
				if (opcode.equals("LW")) {
					Object[] timeAndValue = simulator
							.searchCache(simulator.registerFile[simulator.map
									.get(operands[1])]
									+ Integer.parseInt(operands[2]));
					result =((Integer)timeAndValue[1]).intValue();
				System.out.println("RESULT"+ result);
					updateStation(operands[0], result, i);
					System.out.println(result);
				
					currentCycle = cycles + (Integer) timeAndValue[0];
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add((Integer) timeAndValue[0]);
					}
					executeArray.remove(i);
	
			}
				if (opcode.equals("SW")) {
					
					
					
					int time = simulator.writeToMemory(
							simulator.registerFile[simulator.map.get(operands[1])]
									+ Integer.parseInt(operands[2]),
							simulator.registerFile[simulator.map
									.get(operands[0])]);
				
					
					System.out.println("MEMOOOOOOOOOOOOOOOOOOO");
					System.out.println(simulator.registerFile[simulator.map.get(operands[0])]);
					currentCycle = cycles + time;
					allCycles.add(currentCycle);
					if(simulator.reservationStations.RS.size() == simulator.reservationStations.size) {//RS full
						smallest.add(time);
					}
					executeArray.remove(i);
					removeKey(operands[0]);
				}
				if (opcode.equals("JMP")) {
					

					PC =   simulator.registerFile[simulator.map.get(operands[0])]+Integer.parseInt(operands[2])
							+ Integer.parseInt(operands[1])+1;
					fetchArray.clear();
					decodeArray.clear();
					dispatchArray.clear();


					branchHandle("JMP",executeArray.get(i));
					i=0;

					branchString.clear();
					branchArray.clear();
					branchFlag = false;
					
					cycles+=defaultLatency;
					
//					currentCycle = cycles + defaultLatency;
					allCycles.add(cycles);
					//executeArray.remove(i);
					//fetch();

				}
				if (opcode.equals("BEQ")) {

					if (simulator.registerFile[simulator.map.get(operands[0])] == simulator.registerFile[simulator.map
							.get(operands[1])]) {

						
					
						PC = Integer.parseInt(operands[3])
								+ Integer.parseInt(operands[2])+1;
						fetchArray.clear();
						decodeArray.clear();
						dispatchArray.clear();
						System.out.println(executeArray.get(i));
						 branchHandle("BEQ",executeArray.get(i));
						
						//executeArray.remove(i);
						i=0;
						branchString.clear();
						branchArray.clear();
						branchFlag = false;
					
						cycles+= defaultLatency;
//						currentCycle = cycles + defaultLatency;
						allCycles.add(cycles);
						
						fetch();
					} else {
						branchHandleNot("BEQ");
						branchArray.clear();
						branchString.clear();
						branchFlag = false;
						executeArray.remove(i);
					}

				}
				if (opcode.equals("JALR")) {
					simulator.registerFile[simulator.map.get(operands[0])] = Integer.parseInt(operands[2]) + 1;
					PC = simulator.registerFile[simulator.map.get(operands[1])];
					fetchArray.clear();
					decodeArray.clear();
					dispatchArray.clear();

			 branchHandle("JALR",executeArray.get(i));
					i=0;

					branchString.clear();
					branchArray.clear();
					branchFlag = false;
					
					cycles+=defaultLatency;
//					currentCycle = cycles + defaultLatency;
					allCycles.add(cycles);
					//executeArray.remove(i);
					//fetch();
				}

				if (opcode.equals("RET")) {
					PC = simulator.registerFile[simulator.map.get(operands[0])];
					fetchArray.clear();
					decodeArray.clear();
					dispatchArray.clear();

					branchHandle("RET",executeArray.get(i));
					i=0;

					branchString.clear();
					branchArray.clear();
					branchFlag = false;
					
					cycles+=defaultLatency;
//					currentCycle = cycles + defaultLatency;
					allCycles.add(cycles);
					//executeArray.remove(i);
					//fetch();

				}
				int small = calcSmallest(smallest);
				if(small!=-1) {
					cycles+=small;
				}
				
				if(i>=0)
				i = i - 1;
			}
		}
		
		commit(allCycles);

	}

	private static int calcSmallest(ArrayList<Integer> smallest) {
		if(smallest.isEmpty())
			return -1;
		else {
			int small = Integer.MAX_VALUE;
			for (int i = 0; i < smallest.size(); i++) {
				if (small > smallest.get(i))
					small = smallest.get(i);
			}
			return small;
		}
	}

	static void commit(ArrayList<Integer> allCycles) {

		System.out.println(simulator.rob.toString());
		while (!simulator.rob.ROB.isEmpty() && simulator.rob.getHead().flag) {

			String opcode = simulator.rob.ROB.getFirst().instructionType;
			if (!(opcode.equals("BEQ") || opcode.equals("JMP")
					|| opcode.equals("JALR") || opcode.equals("RET") ||opcode.equals("SW") )) {

				int result = simulator.rob.getHead().value;
				int place = simulator.map.get(simulator.rob.getHead().register);
				simulator.emptyReigsters.add(simulator.rob.getHead().register);
				simulator.registerFile[place] = result;
			}

			simulator.rob.ROB.removeFirst();

			clearRS();

		}

		for (int i = 0; i < simulator.registerFile.length; i++) {
			System.out.print(simulator.registerFile[i] + ",");
		}

		// see final Cycle Time
		for (int i = 0; i < allCycles.size(); i++) {
			if (getFinalCycles() < allCycles.get(i))
				finalCycle = allCycles.get(i);
		}
		finalCycle += 2;

		System.out.println(getFinalCycles());

		System.out.println(simulator.emptyReigsters.toString());
		System.out.println(simulator.rob.toString());
		System.out.println(simulator.reservationStations.toString());

	}

	public static void main(String[] args) {


		//int[] registerFile = { 0, 0, 0, 0, 0, 0, 0, 0 }; 

		int completedInstructions = 0;
		// initialization will be put in a different location

		ReservationStation reservationStations = new ReservationStation(5, 1,
				4, 10);
		ROB rob = new ROB(10);

		Hashtable<Integer, Object> memory = new Hashtable<Integer, Object>(100);
		Hashtable<String, Integer> map = new Hashtable<String, Integer>();
		ArrayList<ArrayList<String>> registerMap = new ArrayList<ArrayList<String>>();
		LinkedList<String> emptyReigsters = new LinkedList<String>();
//branch sw
		for (int i = 16; i < 26; i++) {
			emptyReigsters.add("R" + i);
		}

		ArrayList<ArrayList<String>> mapList = new ArrayList<ArrayList<String>>(
				10);

		Simulator sim = new Simulator();
		sim.completedInstructions = completedInstructions;
		sim.emptyReigsters = emptyReigsters;
		sim.rob = rob;
	
		sim.map = map;
		
		sim.registerMap = mapList;
		sim.reservationStations = reservationStations;
		
		sim.map.put("R0", 0);
		sim.map.put("R1", 1);
		sim.map.put("R2", 2);
		sim.map.put("R3", 3);
		sim.map.put("R4", 4);
		sim.map.put("R5", 5);
		sim.map.put("R6", 6);
		sim.map.put("R7", 7);
		
		Cache[] caches = new Cache[4];
		caches[0] = new Cache(16, 4, 1, 1, 0);
		caches[1] = new Cache(16, 4, 1, 1, 0);
		caches[2] = new Cache(24, 6, 1, 2, 0);
		caches[3] = new Cache(32, 8, 1, 4, 0);
		sim.caches = caches;
		sim.cacheLevels = 3;
		sim.memoryCapacity = 512;
		sim.memoryAccessTime = 8;
		int[] registerFile = { 2, 2, 2, 2, 2, 2, 2, 2 };
		sim.registerFile = registerFile;
		sim.programStartingAddress = 0;
		sim.memory = memory;
		sim.program = new String[7];/*
		memory.put(0, "LW R1,R2,11");
		memory.put(1, "ADD R3,R3,R1");
		memory.put(2, "BEQ R3,R4,1");
		memory.put(3, "RET R0");
		memory.put(4, "MUL R5,R6,R7");
		memory.put(5, "JALR R6,R5");
		memory.put(6, "NAND R2,R1,R0");
		memory.put(7, "SW R4,R3,7");
		memory.put(8, "ADD R2,R6,R0");
		memory.put(9, "DIV R4,R5,R7");
		memory.put(10,"JMP R0,1");
		memory.put(11,"ADDI R1,R2,1");
		memory.put(12,"SW R4,R0,20");
		memory.put(13,"LW R6,R0,20");*/
		
		memory.put(0, "NAND R2,R3,R3");
		memory.put(1, "ADD R4,R1,R2");
		memory.put(2, "ADDI R3,R3,100");
		memory.put(3, "MUL R6,R2,R2");
		memory.put(4, "DIV R7,R3,R2");
		memory.put(5, "DIV R3,R3,R1");
		memory.put(6, "MUL R3,R3,R1");

		
		memory.put(15, new Integer(12));
		//memory.put(0, arg1)
		PC = sim.programStartingAddress;
		// / memory.put(3, "SUB R5,R0,R3");
		// sim.addTime=false;
		simulator = sim;
		// memory.put(0, "ADDI R1,R1,5");
		// memory.put(1, "ADDI R2,R2,5");
		// memory.put(2, "MUL R5,R1,R2");
		// // for (int i = 0; i < data.length; i++) {
		// decode("Add R0,R1,R2");
		// decode("Add R3,R0,R1");
		// decode("Add R0,R0,R0");
		// }

		fetch();
		// decode();
	}

	static boolean containsValue(String s) {

		for (int i = 0; i < simulator.registerMap.size(); i++) {

			if (simulator.registerMap.get(i).get(1).equalsIgnoreCase(s))
				return true;

		}
		return false;
	}

	static boolean containsKey1(String s) {

		for (int i = 0; i < simulator.registerMap.size(); i++) {

			if (simulator.registerMap.get(i).get(0).equalsIgnoreCase(s)){
		
				
				
				return true;
			}
		}
		return false;
	}

	static String getKey(String s) {
		String x = "-1";
		for (int i = 0; i < simulator.registerMap.size(); i++) {

			if (simulator.registerMap.get(i).get(1).equals(s))
				x = simulator.registerMap.get(i).get(0);

		}
		return x;
	}

	static boolean removeKey(String s) {

		for (int i = 0; i < simulator.registerMap.size(); i++) {

			if (simulator.registerMap.get(i).get(0).equals(s)) {
				simulator.registerMap.remove(simulator.registerMap.get(i));
				simulator.map.remove(simulator.map.get(s));
				return true;
			}
		}
		return false;
	}

	static void clearRS() {
		for (int i = 0; i < simulator.reservationStations.RS.size(); i++) {
			if (simulator.reservationStations.RS.get(i).status.equals("Done")) {
				simulator.reservationStations.RS
						.remove(simulator.reservationStations.RS.get(i));
				simulator.reservationStations.size++;
			}
		}
	}

	static int getCycles() {
		return cycles;
	}

	static int getFinalCycles() {
		return finalCycle;
	}

	static boolean checkOP(String op2) {
		if (op2 == null)
			return false;
		else
			for (int i = 0; i < op2.length(); i++) {
				if (op2.charAt(i) == 'R')
					return true;
			}

		return false;
	}

	static int branchHandle(String s, String instruction) {
int hob = 0;
		for (int j = 0; j < simulator.rob.ROB.size(); j++) {
			for (int j2 = 0; j2 < branchArray.size(); j2++) {

				if (simulator.rob.ROB.get(j).instructionID == branchArray
						.get(j2)) {

					for (int i = 0; i < simulator.registerMap.size(); i++) {
						if (simulator.registerMap
								.get(i)
								.get(0)
								.equalsIgnoreCase(
										simulator.rob.ROB.get(j).register))
							simulator.registerMap.remove(simulator.registerMap
									.get(i));
					}

					simulator.emptyReigsters
							.add(simulator.rob.ROB.get(j).register);
					simulator.rob.ROB.remove(simulator.rob.ROB.get(j));
				}
			}
		}
		for (int j = 0; j < simulator.reservationStations.RS.size(); j++) {
			for (int j2 = 0; j2 < branchArray.size(); j2++) {

				if (simulator.reservationStations.RS.get(j).instructionID == branchArray
						.get(j2)) {

					simulator.reservationStations.RS
							.remove(simulator.reservationStations.RS.get(j));
					simulator.reservationStations.size++;
				}
			}
		}

		for (int j = 0; j < simulator.rob.ROB.size(); j++) {
			if (simulator.rob.ROB.get(j).instructionType.equals(s)) {
				simulator.rob.ROB.remove(simulator.rob.ROB.get(j));
			}
		}


	
		for (int j = 0; j < branchString.size(); j++) {
			for (int j2 = 0; j2 < executeArray.size(); j2++) {
				if(branchString.get(j).equals(executeArray.get(j2))){
					System.out.println(executeArray.get(j2));
					executeArray.remove(executeArray.get(j2));
				
					hob++;
				}
				}
		}
	
		for (int i = 0; i < executeArray.size(); i++) {
			if(executeArray.get(i).equals(instruction))
				executeArray.remove(executeArray.get(i));
		}
		
	return hob;
	}

	static void branchHandleNot(String s) {

/*		for (int j = 0; j < simulator.rob.ROB.size(); j++) {
			if (simulator.rob.ROB.get(j).instructionType.equals(s)) {

				simulator.rob.ROB.remove(simulator.rob.ROB.get(j));
			}
		}*/

		for (int j = 0; j < simulator.reservationStations.RS.size(); j++) {
			if (simulator.reservationStations.RS.get(j).operation.equals(s)) {
				simulator.reservationStations.RS
						.remove(simulator.reservationStations.RS.get(j));
				simulator.reservationStations.size++;
			}
		}
	}
}